
package com.insigma.business.qrcode.soldier._provider.impl;


import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.converter.CommonDataDictProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier._provider.CommonDataDictProvider;
import com.insigma.business.qrcode.soldier.dto.CommonDataDictBSearcher;
import com.insigma.business.qrcode.soldier.entity.CommonDataDictDO;
import com.insigma.business.qrcode.soldier.entity.CommonDataDictDTO;
import com.insigma.business.qrcode.soldier.entity.CommonDataDictSearchRequest;
import com.insigma.business.qrcode.soldier.entity.CommonDataDictUpdateRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CommonDataDictInsertRequest;
import com.insigma.business.qrcode.soldier.service.CommonDataDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonDataDictProvider")
public class CommonDataDictProviderImpl extends BaseProviderImpl implements CommonDataDictProvider {

    @Autowired
    private CommonDataDictService commonDataDictService;


    @Override
    public BasePagingResponse<List<CommonDataDictDTO>> searchCommonDataDict(final CommonDataDictSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonDataDictDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonDataDict的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonDataDictBSearcher commonDataDictBSearcher = CommonDataDictProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonDataDictDO> commonDataDictDOList = commonDataDictService.search(commonDataDictBSearcher);
                final List<CommonDataDictDTO> commonDataDictDTOList = CommonDataDictProviderConverter.convertResponseDOList2DTOList(commonDataDictDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonDataDictDTOList, searchRequest,commonDataDictBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_DATA_DICT_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonDataDictDTO> getCommonDataDict(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonDataDictDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonDataDict的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonDataDictDO commonDataDictDO = commonDataDictService.get((String)baseGetRequest.getId());
                final CommonDataDictDTO remoteObj = CommonDataDictProviderConverter.convertResponseDO2DTO(commonDataDictDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_DATA_DICT_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonDataDict(final CommonDataDictInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonDataDict的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CommonDataDictDO commonDataDictDO = CommonDataDictProviderConverter.convertRequestIO2DO(insertRequest);
                commonDataDictService.insert(commonDataDictDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonDataDictDO.getModuleName());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_DATA_DICT_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonDataDict(final CommonDataDictUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonDataDict的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CommonDataDictDO commonDataDictDO = CommonDataDictProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = commonDataDictService.update(commonDataDictDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_DATA_DICT_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCommonDataDict(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonDataDict的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = commonDataDictService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_DATA_DICT_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
